package com.tjsinfo.tjsoa.system.action;

import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserDepartmentBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserDutyBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserPostBO;
import com.tjsinfo.tjsoa.system.bo.ITSystemUserRoleGroupBO;
import com.tjsinfo.tjsoa.system.vo.TSystemPrivategroup;
import com.tjsinfo.tjsoa.system.vo.TSystemRolesGroup;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.TSystemUserDepartment;
import com.tjsinfo.tjsoa.system.vo.TSystemUserDuty;
import com.tjsinfo.tjsoa.system.vo.TSystemUserPost;
import com.tjsinfo.tjsoa.system.vo.TSystemUserRoleGroup;

/**
 * 查询一个用户根据ID
 * @author Administrator
 *
 */
public class SelectUserByNameAction extends Action{
	private IJDBCDao jdbcdao;

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		String userName = request.getParameter("userName"); 
		if (userName!=null && !"".equals(userName)) {
			String _value = "";
			if (userName.indexOf(";")!=-1) {
				_value = userName.substring(userName.lastIndexOf(";")+1,userName.length());
			}else {
				_value = userName;
			}
			if (_value!=null && !"".equals(_value)) {
				JSONArray array = new JSONArray();
				JSONObject totalproperty = new JSONObject();
				int j = 0;
				//for (int i = 1; i <= 5; i++) {
				//	String mssql = ToolKit.getProperties("com/tjsinfo/tjsoa/mail/properties/systemResource.properties", this).getProperty(String.valueOf(i));
					String sql = "select top 10 userId,userName from dbo.t_system_user where userName like '%"+_value+"%' and state=0";
					ResultSet rs = jdbcdao.executeQuary(sql);
					
					while(rs.next()){
						JSONObject object = new JSONObject();
						object.put("userId", rs.getInt("userId")+","+rs.getString("userName")+",1@");
						object.put("userName", rs.getString("userName")+";");
						array.put(object);
						j++;
						if (j==10) {
							break;
						}
					}
				//}
				totalproperty.put("root", array);
				response.getWriter().print(totalproperty);
			}
			
		}

		return null;
	}

	public IJDBCDao getJdbcdao() {
		return jdbcdao;
	}

	public void setJdbcdao(IJDBCDao jdbcdao) {
		this.jdbcdao = jdbcdao;
	}
	
	
}